/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProveedorView.java
 *
 * Created on 03-dic-2009, 21:14:22
 */

package vista;

import controlador.IProveedorController;
import modelo.IProveedor;
import modelo.IProveedorModel;
/**
 *
 * @author Juanma
 */
public class ProveedorView extends javax.swing.JFrame implements IProveedorView {

    private IProveedorModel model;
    private IProveedorController controller;
    
    private ProveedoresTableModel tableModel;
    private JPanelProveedor panelProveedor;
    /** Creates new form ProveedorView */
    public ProveedorView() {
        tableModel=new ProveedoresTableModel();
        initComponents();

        this.panelProveedor=new JPanelProveedor(this);
        this.panelabajo.add(this.panelProveedor);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableProveedores = new javax.swing.JTable();
        panelabajo = new javax.swing.JPanel();

        setTitle("Gestión de proveedores");
        setName("frame0"); // NOI18N

        jTableProveedores.setModel(tableModel);
        jTableProveedores.setUpdateSelectionOnSort(false);
        jTableProveedores.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableProveedoresMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableProveedores);

        panelabajo.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        panelabajo.setLayout(new javax.swing.BoxLayout(panelabajo, javax.swing.BoxLayout.LINE_AXIS));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(panelabajo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(panelabajo, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTableProveedoresMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableProveedoresMouseClicked
        ProveedoresTableModel ctm=(ProveedoresTableModel)this.jTableProveedores.getModel();
        IProveedor proveedor=ctm.getRow(this.jTableProveedores.getSelectedRow());
        this.panelProveedor.setProveedor(proveedor);
    }//GEN-LAST:event_jTableProveedoresMouseClicked
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ProveedorView().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableProveedores;
    private javax.swing.JPanel panelabajo;
    // End of variables declaration//GEN-END:variables

    public IProveedorController getController() {
        return controller;
    }

    public void setController(IProveedorController controller) {
        this.controller=controller;
    }

    public IProveedorModel getModel() {
        return model;
    }

    public void setModel(IProveedorModel model) {
        this.model=model;
    }

    public void dataModelChanged() {
        refresh();
    }

    public void display() {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                setVisible(true);
                refresh();
            }
        });
    }

    protected void refresh() {
        this.tableModel.setContactos(getModel().obtenerProveedor());
    }
    
    protected void fireNuevoContactoGesture(int ID, String nombre, boolean defecto) {
        getController().nuevoProveedorGesture(ID, nombre, defecto);
    }

    protected void fireActualizaContactoGesture(int ID, String nombre, boolean defecto) {
        getController().actualizaProveedorGesture(ID, nombre, defecto);
    }

    protected void fireBorraContactoGesture(int ID){
        getController().borraProveedorGesture(ID);
    }
    
}
